package com.`as`.common

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context

/**
 * descrition: 剪贴板相关
 */
/**
 * Copy the text to clipboard.
 *
 * 复制文本到剪贴板
 *
 * @param text The text.
 */
fun Context.copyText(text: CharSequence?) {
    val cm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    cm.setPrimaryClip(ClipData.newPlainText(packageName, text))
}

/**
 * Return the text for clipboard.
 * 获取剪贴板的文本
 * @return the text for clipboard
 */
fun Context.getText(): CharSequence {
    val cm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    val clip = cm.primaryClip
    if (clip != null && clip.itemCount > 0) {
        val text = clip.getItemAt(0).coerceToText(this)
        if (text != null) {
            return text
        }
    }
    return ""
}

/**
 * Clear the clipboard.
 * 清除剪贴板
 */
fun Context.clear() {
    val cm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    cm.setPrimaryClip(ClipData.newPlainText(null, ""))
}